package annotation.inherited;

import java.lang.annotation.Annotation;

/**
 * 可以看到Son.java没有任何注解，
 * 因为InheritedAnnotation注解有@Inherited，Son继承了Father的InheritedAnnotation注解,
 * MyAnnotation注解没有@Inherited，Son没有继承MyAnnotation注解
 */
public class InheritedTest {

    public static void main(String[] args) {

        System.out.println("----------------------father----------------------");
        Class<Father> fatherClass = Father.class;
        // 获得当前Class(Son)所有的所有注解
        Annotation[] fatherAnnotations = fatherClass.getAnnotations();
        System.out.println("当前类的注解数量：" + fatherAnnotations.length); // 当前类的注解数量：1

        // @annotation.inherited.InheritedAnnotation(value=inherited)
        // @annotation.inherited.MyAnnotation(value=[myAnnotation])
        for (Annotation annotation : fatherAnnotations) {
            System.out.println(annotation);
        }

        // 判断是否有特定的Annotation
        boolean fatherAnnotationPresent = fatherClass.isAnnotationPresent(InheritedAnnotation.class);
        if (fatherAnnotationPresent) {
            InheritedAnnotation inheritedAnnotation = fatherClass.getAnnotation(InheritedAnnotation.class);
            String value = inheritedAnnotation.value();
            System.out.println(value); // inherited
        }

        boolean fatherAnnotationPresent2 = fatherClass.isAnnotationPresent(MyAnnotation.class);
        if (fatherAnnotationPresent) {
            MyAnnotation myAnnotation = fatherClass.getAnnotation(MyAnnotation.class);
            String[] value = myAnnotation.value();
            for (String str : value) {
                System.out.println(str); // myAnnotation
            }
        }

        System.out.println("----------------------son----------------------");
        Class<Son> sonClass = Son.class;
        // 获得当前Class(Son)所有的所有注解
        Annotation[] sonAnnotations = sonClass.getAnnotations();
        System.out.println("当前类的注解数量：" + sonAnnotations.length); // 当前类的注解数量：1

        // @annotation.inherited.InheritedAnnotation(value=inherited)
        for (Annotation annotation : sonAnnotations) {
            System.out.println(annotation);
        }

        // 判断是否有特定的Annotation
        boolean sonAnnotationPresent = sonClass.isAnnotationPresent(InheritedAnnotation.class);
        if (sonAnnotationPresent) {
            InheritedAnnotation inheritedAnnotation = sonClass.getAnnotation(InheritedAnnotation.class);
            String value = inheritedAnnotation.value();
            System.out.println(value); // inherited
        }

    }

}
